package controller;

import constant_tools.TokenUtils;
import constant_tools.UserTools;
import controller.result.Code;
import controller.result.Msg;
import controller.result.Result;
import domain.*;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import service.FileService;
import service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Map;

@RestController
@RequestMapping("users")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private FileService fileService;

    @PostMapping("/uploadHistory")
    public Result getUploadHisttory(@RequestBody Map<String, Integer> requestBody){
        int uid = requestBody.get("uid");
        ArrayList<VirtualFile> list = userService.getUploadHistoryByUid(uid);
        return new Result(Code.GET_OK,list);
    }

    @PostMapping("/delete")
    public Result deleteUser(@RequestBody Map<String, Integer> requestBody) {
        int uid = requestBody.get("uid");
        fileService.setNewOwner(uid,1);
        userService.deleteUser(uid);
        return null;
    }

    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody updatedUser updatedUser){
        VirtualUser user = new VirtualUser();
        user.setEmail(updatedUser.getEmail());
        user.setState(updatedUser.getState());
        user.setUname(updatedUser.getUname());
        user.setUid(updatedUser.getUid());

        userService.updateUser(user);
        return null;
    }

    @GetMapping("/getAll")
    public Result getAllUsers(){
        ArrayList<VirtualUser> l  = new ArrayList<>();
        l=userService.getAllusers();
        return new Result(Code.GET_ALL_USERS_SUCCESS,l);
    }

    @PostMapping("/resetInfo")
    public Result userResetInfo(@RequestBody EditForm editForm){
        String uname = editForm.getUname();
        String email = editForm.getEmail();
        String psword = editForm.getPassword();

        int uid = editForm.getUid();
        VirtualUser virtualUser = userService.getByuid(uid);
        if(virtualUser==null){
            return null;
        }
        virtualUser.setPsword(psword);

        psword = UserTools.addSalt(virtualUser);
        if(!psword.equals(userService.getByuid(uid).getPsword())){
            return new Result(Code.PSWORD_WRONG,null,Msg.PSWORD_WRONG);
        }

        //更新用户信息
        userService.resetUserInfo(uid,uname,email);
        virtualUser = userService.getByuid(uid);
        return new Result(Code.LOGIN_SUCCESS,virtualUser,Msg.LOGIN_SUCCESS);

    }

    @PostMapping("/login")
    public Result userLogin(@RequestBody LoginForm loginForm, HttpServletRequest request){

        Vir_User_Token vir_user_token;
        String username = loginForm.getUsername();
        String psword = loginForm.getPassword();

        String token="";

        //判断用户名是否存在
        if(!userService.haveUser(username)){
            return new Result(Code.USER_NOT_EXIST,null, Msg.USER_NOT_EXIST);
        }

        VirtualUser virtualUser = userService.getByname(username);

        virtualUser.setPsword(psword);

        System.out.println(virtualUser);

        psword = UserTools.addSalt(virtualUser);

        //判断密码是否正确
        if(!psword.equals(userService.getPsword(username))){
            return new Result(Code.PSWORD_WRONG,null,Msg.PSWORD_WRONG);
        }



        token = TokenUtils.sign(username,psword);

        virtualUser.setPsword(null);
        vir_user_token = new Vir_User_Token();
        vir_user_token.setToken(token);
        vir_user_token.setVirtualUser(virtualUser);


        return new Result(Code.LOGIN_SUCCESS,vir_user_token,Msg.LOGIN_SUCCESS);
    }



    @PostMapping("/register")
    public Result register(@RequestBody EntityPerson entityPerson){

        System.out.println(entityPerson.toString());

        VirtualUser virtualUser = new VirtualUser();

        String name = entityPerson.getName();
        String password = entityPerson.getPassword();
        String email = entityPerson.getEmail();

        if(userService.haveUser(name)||userService.haveEmail(email)){
            return new Result(Code.USER_EXIST,null,Msg.USER_EXIST);
        }

        virtualUser.setUname(name);
        virtualUser.setPsword(password);
        virtualUser.setEmail(email);
        virtualUser.setState(1);
        virtualUser.setCreate_date_time(new Timestamp(System.currentTimeMillis()/1000*1000));

        System.out.println(virtualUser);

        String newpass = UserTools.addSalt(virtualUser);
        virtualUser.setPsword(newpass);

        //检测邮箱合法性和是否已存在

        //插入数据
        userService.addUser(virtualUser);

        return new Result(Code.REGISTER_SUCCESS,null,Msg.REGISTER_SUCCESS);
    }

}
